<template>
	<view>
		<!-- 搜索框 -->
		<view class="serch-box">
			<uni-search-bar  
				@input="input" 
				:radius="100" 
				:cancelButton="none" 
				placeholder="搜索内容"></uni-search-bar>
			
		</view>
		<view class="sugg-list" v-if="searchList.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchList" :key="i" @click="goToGoodsDetail(item)">
				<view class="goods-name">
					{{item.goods_name}}
				</view>
				<uni-icons type="forward" size="16" >
					
				</uni-icons>
			</view>
		</view>
		<view class="history-box" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHis"></uni-icons>
			</view>
			<view class="histroy-list">
				<uni-tag  :text="item"  v-for="(item,i) in histroies" :key="i" @click="goToGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer:null,//防抖关键词
				kw:"",//搜索关键词
				searchList:[],//搜索关键词返回数组
				hosioryList:["a","你好","apple"],//搜索历史的数据
				
			}
		},
		onLoad() {
			this.hosioryList =JSON.parse(uni.getStorageSync("kw"))  
		},
		computed:{
			histroies(){
				return [...this.hosioryList].reverse()
			}
		},
		methods: {
			//用户输入
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(()=>{
					this.kw = e
					this.getSearchList()
				},500)
			},
			async getSearchList(){
				if (this.kw.length === 0) {
					this.searchList = []
					return
				}
				const {data:res} =	await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if (res.meta.status !== 200) return uni.showMsg()
				this.searchList = res.message
				this.saveKwSearchList()
			},
			goToGoodsDetail(item){
				uni.navigateTo({
					url:'/subpkg/good_detail/good_detail?goods_id'+item.goods_id
				})
				
			},
			saveKwSearchList(){
				const set = new Set(this.hosioryList)
				set.delete(this.kw)
				set.add(this.kw)
				console.log(set);
				this.hosioryList = Array.from(set)
				uni.setStorageSync("kw",JSON.stringify(this.hosioryList))
			},
			cleanHis(){
				this.hosioryList = []
				uni.setStorageSync('kw','[]')
			},
			goToGoodsList(kw){
				console.log(kw,"00088");
				uni.navigateTo({
					url:"/subpkg/goods_list/goods_list?query" + kw
				})
				
			}
		}
	}
</script>

<style lang="scss">
	.serch-box{
		position: sticky;
		top: 0;
		z-index: 999;
		
	}
	.sugg-list{
		padding: 0 5px;
		
		.sugg-item{
			display: flex;
			justify-content: space-between;
			align-items: center;
			font-size: 12px;
			padding: 13px 0;
			border: 1px solid #efefef;
			
			.goods-name{
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
				margin: 3px;
			}
		
		}
	}

	.history-box{
		padding: 0 5px;
		
		.history-title{
			display: flex;
			justify-content: space-between;
			height: 40px;
			align-items: center;
			font-size: 13px;
			border-bottom: 1px solid #efefef;
		}
		
		.histroy-list{
			display: flex;
			flex-wrap: wrap;
			margin-top: 5px;
			
			.uni-tag{
				margin-right: 5px;
				margin-bottom: 5px;
			}
		}
	}
</style>
